package com.dq.mail.config;

import com.dq.mail.interceptor.LoginInterceptor;
import com.dq.mail.interceptor.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName: WebConfig
 * @Description: TODO 查询商户
 * @Author: app-backend-public_小强 (QQ:2680825683)
 * @Since: 2023/3/4 21:38
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                // 配置对所有请求路径生效
                .addMapping("/**")
                // 放行所有请求域（默认配置，不配置就是放行所有）
                .allowedOrigins("*")
                // 浏览器是否应该发送凭据
                .allowCredentials(true)
                // 放行所有请求方法（默认只放行GET/HEAD/POST）
                .allowedMethods("*");
    }

    /**
     * 拦截器
     *
     * @param registry 祖册信息初始化
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //全局刷新Token
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
        //正式拦截器
        registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(
                //用户模块  登录 注册
                "/user/register",
                "/user/login",
                "/user/userListTest",
                "/spot/**",
                "/chat/**",
                "/swagger-ui.html",
                "/webjars/**",
                "/swagger-resources/**",
                "/v2/**"
        ).order(1);
    }
}
